<?php

namespace addons\yunfood\controller;

use addons\yunfood\library\Constant;
use addons\yunfood\library\Logic;
use addons\yunfood\model\Config;

/**
 * 公共接口
 */
class Common extends BaseApi
{
    public $noNeedLogin = ['getConfig', 'getMpConfig'];

    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取配置
     */
    public function getConfig()
    {
        $result = [];

        // 基础配置
        $baseConfig = Config::getGroupAttributes(Config::GROUP_BASE);
        $loginMethods = $baseConfig['login_methods'] ?? null;
        $result['login_methods'] = $loginMethods ? explode(',', $loginMethods) : [];
        $result['login_note'] = $baseConfig['login_note'] ?? '';

        // 公众号配置
        $result['mp_login'] = (int)Config::getGroupAttribute(Config::GROUP_MP, 'mp_login', 0);

        // 显示地图
        $result['show_map'] = (int)Config::getGroupAttribute(Config::GROUP_BASE, 'show_map', 0);

        // 样式数据
        $result['style_login_bg'] = cdnurl('/assets/addons/yunfood/img/login-bg.png', true);
        $result['style_mine_bg'] = cdnurl('/assets/addons/yunfood/img/mine-bg.png', true);
        $result['style_order_bg'] = cdnurl('/assets/addons/yunfood/img/order-bg.png', true);
        $result['style_detail_bg'] = cdnurl('/assets/addons/yunfood/img/detail-bg.png', true);

        $this->success(__('success'), $result);
    }

    /**
     * 获取微信网页配置
     */
    public function getMpConfig()
    {
        $url = $this->request->post('url', null, null);
        if (! Config::getGroupAttribute(Config::GROUP_MP, 'app_id')) {
            $this->error('暂不支持', null, Constant::IGNORE_ERROR);
        }

        $result = null;
        try {
            $result = Logic::getMpConfigData($url);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('操作成功', $result);
    }
}
